package mess;

import java.lang.reflect.Array;
import java.rmi.Remote;
import java.util.Arrays;

public class LeeCode_59 {
    public static void main(String[] args) {
        generateMatrix(2);
    }
    private static int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int remain = n;
        int num = 1;
        int temp = n * n;
        while (remain > 0){
            int start = (n - remain) / 2;
            for (int j = start; j < n - start; j++) {
                res[start][j] = num;
                num++;
            }
            if (num > temp)
                break;
            for (int i = start + 1; i < n - start; i++) {
                res[i][n - start - 1] = num;
                num++;
            }
            if (num > temp)
                break;
            for (int j = n - start - 2; j >= start; j--) {
                res[n - start - 1][j] = num;
                num++;
            }
            for (int i = n - start - 2; i > start; i--) {
                res[i][start] = num;
                num++;
            }
            remain -= 2;
        }
        return res;
    }
}
